﻿using B.S.DataBase.MyProject.Domain;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using B.S.DataBase.MyWrite.API.Appicant.Command.Roles;
using MediatR;

namespace B.S.DataBase.MyWrite.API.Appicant.CommandHandler.Roles
{
    public class RemoveUserRoleCommandHandler : IRequestHandler<RemoveUserRoleCommand, APIResult<string>>
    {
        private readonly IBaseRepository<UserRole> userRoleRepos;

        public RemoveUserRoleCommandHandler(IBaseRepository<UserRole> userRoleRepos)
        {
            this.userRoleRepos = userRoleRepos;
        }

        /// <summary>
        /// 批量删除用户角色关系表
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<APIResult<string>> Handle(RemoveUserRoleCommand request, CancellationToken cancellationToken)
        {
            APIResult<string> result = new APIResult<string>();
            var userroleids = request.UserRoleId.Select(x=> new UserRole { UserRoleId = x }).ToList();
            var res = userRoleRepos.DeleteRemove(userroleids).Result;
            if (res > 0)
            {
                result.Code = APIEnum.删除成功;
                result.Msg = APIEnum.删除成功.ToString();
                result.data = res.ToString();
            }
            else
            {
                result.Code = APIEnum.修改失败;
                result.Msg = APIEnum.修改失败.ToString();
            }
            return await Task.FromResult(result);     
        }
    }
}
